﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Windows
{
    public partial class FormProcedureEdit : DevExpress.XtraEditors.XtraForm
    {
        private readonly IProcedurePersist _procedurePersist = null;
        private readonly TProcedureModel _parentModel = null;
        public FormProcedureEdit(TProcedureModel parentModel)
        {
            _procedurePersist = InstanceFactory.GetProcedurePersist();
            _parentModel = parentModel;
            InitializeComponent();
            LoadModel();
        }

        private void LoadModel()
        {
            if (_parentModel != null)
            {
                this.textEditProcedureNoParent.Text = _parentModel.ProcedureNo;
                this.textEditProcedureNameParent.Text = _parentModel.ProcedureName;
            }
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(this.textEditProcedureNo.Text.Trim()))
            {
                XtraMessageBox.Show("工序代码不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                if (_parentModel != null)
                {
                    _procedurePersist.NewProcedureModel(_parentModel.ProcedureNo, this.textEditProcedureNo.Text.Trim(),
                                                        this.textEditProcedureName.Text.Trim());
                }
                else
                {
                    _procedurePersist.NewProcedureModel(string.Empty, this.textEditProcedureNo.Text.Trim(),
                                                this.textEditProcedureName.Text.Trim());
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}